package com.vgooo.client.vchannel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.vgooo.bean.NewListInfo;
import com.vgooo.bean.NewsDetail;
import com.vgooo.client.R;
import com.vgooo.client.VGActivity;
import com.vgooo.client.ui.VGScrollView;
import com.vgooo.client.ui.VGWebView;
import com.vgooo.client.ui.ViewPagerAdapter;
import com.vgooo.util.DebugTools;
import com.vgooo.util.GSonTools;
import com.vgooo.util.HttpUtils;
import com.vgooo.util.Utils;

public class VideoDetailActivity extends VGActivity {
	
	private ViewPager  pager;
	private NewListInfo videoInfo ;

	private NewsDetail newsInfo ;// 新闻信息

	String parent_id = null;
	private PingDetail pingDetail;

	private TextView pingButton;
	private View titleView;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		needNewProgress = true;

		detailVeiw = inflater.inflate(R.layout.new_detail, null);
		detailVeiw.setVisibility(View.INVISIBLE);
		setContentView(detailVeiw);
		initView();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// 友盟统计功能，打开页面
		MobclickAgent.onResume(this);
		
		Bundle bundle = getIntent().getBundleExtra(NewListInfo.NEWS_TYPE_VIDEO);
		NewListInfo videoInfo = (NewListInfo) bundle.get(NewListInfo.NEWS_TYPE_VIDEO);
//		int id = getIntent().getIntExtra("id", -1);
		if (null==videoInfo || videoInfo.equals(this.videoInfo)) {
			detailVeiw.setVisibility(View.VISIBLE);
			return;
		}
		detailVeiw = inflater.inflate(R.layout.new_detail, null);
		setContentView(detailVeiw);
		initView();
		this.videoInfo = videoInfo;
//		detail.clearView();
		init();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		// 友盟统计功能，页面关闭
		MobclickAgent.onPause(this);
		if (null != pingDetail && pingDetail.isEdit) {
			Utils.hideSoftInput(pager);
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		// pingAdapter.
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		pager = (ViewPager) findViewById(R.id.news_detail_view_pager);

		pingDetail = new PingDetail(cparent, PingActivity.TYPE_VIDEO);
		pingDetail.initView(findViewById(R.id.news_detail_ping));
		titleView = inflater.inflate(R.layout.video_detail_title, null);
	}

	@Override
	public void setupView() {
		log("setupView(): newsInfo = " + newsInfo);

		// TODO Auto-generated method stub
		((TextView) titleView.findViewById(R.id.news_detail_news_title))
				.setText(videoInfo.news_title);
//		((TextView) titleView.findViewById(R.id.news_detail_news_source_tv))
//				.setText(newsInfo.news_source);
//		((TextView) titleView.findViewById(R.id.news_detail_news_author_tv))
//				.setText(newsInfo.news_author);
		((TextView) titleView.findViewById(R.id.news_detail_news_review_tv))
				.setText(videoInfo.news_views);

		WebView webVeiw = new WebView(context);
		webVeiw.addJavascriptInterface(this, "android");
		WebSettings setting = webVeiw.getSettings();
		setting.setUseWideViewPort(false);
		setting.setSupportZoom(false);
		setting.setDefaultTextEncodingName("UTF-8");// 设置编码
		setting.setPluginsEnabled(true);// 是否支持插件
		setting.setJavaScriptEnabled(true);// 设置是否支持javaScript
		webVeiw.loadDataWithBaseURL(null, newsInfo.news_content, "text/html",
				"UTF-8", null);
		// 初始化评论分页信息
		List<View> views = new ArrayList<View>();
//		views.add(scrollView);
		views.add(webVeiw);
//		 views.add(mPullDownView);
		pingButton = (TextView) findViewById(R.id.ping_bottom_layout_button);
		pager.setAdapter(new ViewPagerAdapter(pager, views, pingButton,
				newsInfo.news_ping, pingDetail, null));
		pager.setCurrentItem(1);
		pingDetail.setupView(newsInfo.news_ping, newsInfo.news_id, titleView);

		
		detailVeiw.setVisibility(View.VISIBLE);
	}

	@Override
	public void addListener() {
		// TODO Auto-generated method stub
		
		
	}

	@Override
	public void flashView() {
		// TODO Auto-generated method stub

	}

	@Override
	public void getData() {
		try {
			String urlPath = HttpUtils.BASE_URI + "videodetail?id=" + videoInfo.news_id+"&is_transfer=" + 1;;
				String jsonString = HttpUtils.getJsonString(urlPath);

				Map<String, Object> infos = GSonTools.getObjectMaps(jsonString);
				log("infos = " + infos);
				newsInfo = new NewsDetail();
				newsInfo.news_id = videoInfo.news_id ;
				newsInfo.news_content = (String) infos.get("content");
				newsInfo.news_ping = (String) infos.get("video_ping");
				videoInfo.news_views = (String) infos.get("v_views");
				log("newsInfo = " + newsInfo);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			throw new RuntimeException(e);
		}

	}

	@Override
	public void doClick(View v) {

		switch (v.getId()) {

		default:
			if (pingDetail != null) {
				pingDetail.doClick(v);
			} else {
				log("doClick() 未定义控件单击后的事件......" + v.getId());
			}
			break;
		}
	}

	/**
	 * 打印调试信息
	 * 
	 * @param info
	 */
	private void log(String info) {
		// Log.i(this.getClass().getSimpleName(), info);
		DebugTools.log(this.getClass().getSimpleName(), info);
	}

}
